﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
using System.ComponentModel;
using System.Windows.Threading;

namespace MS.Common
{
    /// <summary>
    ///
    /// </summary>
    public class CWindowBase : Window
    {
        /// <summary>
        /// 切换窗体样式
        /// 多线程是为了防止闪动现象
        /// </summary>
        public BackgroundWorker bwChangeStyle = null;

        /// <summary>
        /// 当前窗体
        /// </summary>
        public string NowWindow;

        public CWindowBase()
        {
            InitializeStyle();

        }

        /// <summary>
        /// 初始化样式
        /// </summary>
        private void InitializeStyle()
        {
            this.SetResourceReference(Window.StyleProperty, "KsyWinStyle_NoOpeStyle");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosed(EventArgs e)
        {
            //RemoveMask();
            base.OnClosed(e);
        }

        ///// <summary>
        ///// 将回车改为TAB(隧道事件能被捕获
        ///// </summary>
        ///// <param name="e"></param>
        //protected override void OnKeyDown(KeyEventArgs e)
        //{
        //    if (e.Key == System.Windows.Input.Key.Enter)
        //    {
        //        OnMoveNextFocus(e);
        //    }
        //    base.OnKeyDown(e);
        //}

        /// <summary>
        /// 按照 Tab 键顺序将焦点移至下一个可获得焦点的元素
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnMoveNextFocus(KeyEventArgs e)
        {
            // MoveFocus takes a TraveralReqest as its argument.
            TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);
            // Gets the element with keyboard focus.
            UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;
            // Change keyboard focus. 
            if (elementWithFocus != null)
            {
                elementWithFocus.MoveFocus(request);
            }
            e.Handled = true;
        }
    }
}
